package org.jeecg.modules.equipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.commodity.mapper.CommodityMapper;
import org.jeecg.modules.equipment.EquipmentCondition;
import org.jeecg.modules.equipment.entity.Equipment;
import org.jeecg.modules.equipment.entity.EquipmentInstallCondition;
import org.jeecg.modules.equipment.entity.FilterelementReplace;
import org.jeecg.modules.equipment.entity.InstallationOpen;
import org.jeecg.modules.equipment.mapper.*;
import org.jeecg.modules.equipment.model.EquipmentModel;
import org.jeecg.modules.equipment.service.EquipmentService;
import org.jeecg.modules.equipment.service.FilterelementService;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: zhuzhuhong
 * @Date: 2019/11/15 15:50
 * @Description:
 */
@Service
public class EquipmentServiceImpl
        extends ServiceImpl<EquipmentMapper, Equipment>
        implements EquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UserDepartMapper userDepartMapper;
    @Autowired
    private CommodityMapper commodityMapper;
//    private CommodityService commodityService;

    @Autowired
    private ISysDepartService iSysDepartService;
    @Autowired
    private FilterelementService filterelementService;
    @Autowired
    private FilterelementReplaceMapper filterelementReplaceMapper;
    @Autowired
    private InstallationOpentMapper installationOpentMapper;

    @Override
    public List<EquipmentModel> findEquipmentList(EquipmentCondition condition) {
        try {
            List<String> roleCodes = userRoleMapper.selectRoleCodeByUserId(condition.getUserId());
            if (!roleCodes.contains("admin")) {
                List<String> depIds = userDepartMapper.selectDepartIdByUserId(condition.getUserId());
                condition.setDepIds(depIds);
            }
            return equipmentMapper.selectEquipmentList(condition);

        } catch (Exception e) {
            throw e;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean installFilterCartridge(EquipmentInstallCondition condition) {
        //1、拆分数据,取出设备相关的数据
        Equipment equipment = this.fromEquipmentInstallConditionToEquipment(condition);
        boolean updateResult = this.updateById(equipment);

        // TODO: 2019/11/22 2、新增滤芯的安装记录
        List<FilterelementReplace> filterelementReplaces = this.fromEquipmentInstallConditionToFilterelementReplace(condition);
        int result = filterelementReplaceMapper.insertFilterelementReplaces(filterelementReplaces);
        // TODO: 2019/11/22 3、新增客户设备权限表数据,作业
        InstallationOpen installationOpen = this.fromEquipmentInstallConditionToInstallationOpen(condition);
        int insert = installationOpentMapper.insert(installationOpen);
        return updateResult && (result > 0) && (insert > 0);

    }

    private InstallationOpen fromEquipmentInstallConditionToInstallationOpen(EquipmentInstallCondition condition) {
        InstallationOpen installationOpen = new InstallationOpen();
        installationOpen.setEquipmentId(condition.getEquipmentId());
        installationOpen.setOpenId(UUIDGenerator.generate());
        installationOpen.setClientId(condition.getClientId());
        installationOpen.setCreateBy(condition.getCreateBy());
        installationOpen.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        installationOpen.setOpen(1);
        return installationOpen;
    }


    private Equipment fromEquipmentInstallConditionToEquipment(EquipmentInstallCondition condition) {
        Equipment equipment = new Equipment();
        equipment.setCreateBy(condition.getCreateBy());
        equipment.setClientId(condition.getClientId());
        //通过数据库查询商品ID
        equipment.setCommodityId(commodityMapper.selectCommodityIdByCommodityName(condition.getCommodityName()));
        equipment.setCreateTime(condition.getCreateTime());
        //通过数据库查询部门ID
        equipment.setDepartId(iSysDepartService.getDepartIdByDepartName(condition.getDepartName()));
        //将设备启用状态设为启用
        equipment.setEnabled(1);
        equipment.setEquipmentId(condition.getEquipmentId());
        equipment.setFilterelementType(condition.getFilterelementType());
        equipment.setIds(condition.getIds());
        equipment.setLeasestate(condition.getLeasestate());
        equipment.setLossratio(condition.getLossratio());
        equipment.setMainboard(condition.getMainboard());
        equipment.setRemarks(condition.getRemarks());
        return equipment;
    }

    private List<FilterelementReplace> fromEquipmentInstallConditionToFilterelementReplace(EquipmentInstallCondition condition) {
        //查询当前设备Id所关联的滤芯Id
        List<String> list = filterelementService.findFitlerelementIdByEquipmentId(condition.getEquipmentId());
        List<FilterelementReplace> filterelementReplaces = new ArrayList<>(list.size());
        for (String s : list) {
            FilterelementReplace filterelementReplace = new FilterelementReplace();
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            //创建人取当前登录人
            filterelementReplace.setCreateBy(user.getRealname());
            filterelementReplace.setRecordId(UUIDGenerator.generate());
            //创建日期取系统 当前时间
            Timestamp nowTime = Timestamp.valueOf(LocalDateTime.now());
            filterelementReplace.setCreateTime(nowTime);
            filterelementReplace.setEquipmentId(condition.getEquipmentId());
            filterelementReplace.setInstallationTime(nowTime);
            //滤芯ID
            filterelementReplace.setFilterelementId(s);
            filterelementReplace.setUpdateBy(user.getRealname());
            filterelementReplace.setRemind(0);
            //将对象添加到集合中
            filterelementReplaces.add(filterelementReplace);
        }
        return filterelementReplaces;
    }
}
